package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.*;

public class ShowImageServlet extends HttpServlet {
    private EJBDatabaseQuery ejbdb;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
	doPost(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
    {
	HttpSession session = request.getSession();
	byte[] image_data = null;
	String iid = request.getParameter("iid");

	String tid = request.getParameter("tid");
	String fid = request.getParameter("fid");

	String table_name = null;

	if (iid == null || tid == null || fid == null) {
	    if(session.getAttribute("image_data") != null) { 
		image_data = (byte[])session.getAttribute("image_data");
	    } 
	}
	else {
	    if(tid.equals("1")) {
		table_name = "USER_PHOTOS";
	    }
	    else {
		return;
	    }

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String siid = null;
	    if(session.getAttribute("siid") == null) {
		return;
	    }
	    siid = ""+session.getAttribute("siid");

	    if(MyUserInputCheck.isNumber(fid)) {
		image_data = ejbdb.selectByteArray("SELECT * FROM "+table_name+" WHERE ID="+siid+";", Integer.parseInt(fid));
	    }
	}

	if(image_data == null || image_data.length<1) {
	    return;
	}
	
	response.setContentType("image/gif");
	OutputStream stream = response.getOutputStream();
	stream.write(image_data);
	response.flushBuffer();
	image_data = null;
    }
}






